<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>档案分类</title>
    <jsp:include page="/zheng-admin-plugins/plugins/inc/head.jsp" flush="true"/>
</head>
<style>
    .one{
        float: left;
    }
    #main{
        width: 30%;
        height: 100%;
    }
    #mainright{
        width: 70%;
        height: 100%;
        border-left:  2px;
        box-shadow: 0px 2px 10px #E8E8E8;
    }
    #fonds {
        width: 100%;
    }

    th {
        text-align: left;
        font-size: 15px;
        height: 17px;
    }
    td{
        padding:0px;
    }

    label, input, ul {
        font-size: small !important;
    }
</style>
<body style="font-size:14px;">
<div id="main" class="one">
    <select  id="fonds" class="fonds"  onchange="changFonds()">
        <option value="">请选择全宗</option>
    </select>
    <div style="width:auto;height:auto;overflow: auto;">
        <ul class="ztree" id="tree" ></ul>
    </div>
</div>
<div id="mainright" class="one">

    <shiro:hasPermission name="upms:archivestype:read">
    <form style="margin: 50px;">
        <div class="form-group" style="margin-left: -10px;">
        <shiro:hasPermission name="upms:archivestype:create"><a class="waves-effect waves-button" href="javascript:;" onclick="insertType()" ><i class="zmdi zmdi-plus"></i> 添加分类</a></shiro:hasPermission>
        <shiro:hasPermission name="upms:archivestype:delete"><a class="waves-effect waves-button" href="javascript:;" onclick="delType()" ><i class="zmdi zmdi-close"></i> 删除分类</a></shiro:hasPermission>
        <shiro:hasPermission name="upms:archivestype:create"><a class="waves-effect waves-button" href="javascript:;" id="saveType" onclick="saveType()"><i class="zmdi zmdi-save"></i> 保存分类</a></shiro:hasPermission>
        <shiro:hasPermission name="upms:archivestype:update"><a class="waves-effect waves-button" href="javascript:;" id="updateType" onclick="updateType()"><i class="zmdi zmdi-save"></i> 保存分类</a></shiro:hasPermission><br>
        </div>
        <div class="form-group">
            <label>上级编号：</label><br><br>
            <input id="type1" name="sortNum1" class="form-control" readonly >
        </div>
        <div class="form-group">
            <label>分类编号：</label><br><br>
            <input id="type2" type="number" name="sortNum2" maxlength="40" class="form-control" >
        </div>
        <div class="form-group">
            <label>分类名称：</label><br><br>
            <input id="type3" name="sortName" class="form-control" maxlength="40" autocomplete="off" >
        </div>

    </form>

    </shiro:hasPermission>

</div>
<jsp:include page="/zheng-admin-plugins/plugins/inc/footer.jsp" flush="true"/>
<script>
    //加载全部全宗
    $(function () {
        var fonds = $("#fonds");
        $("#saveType").hide();
        $("#updateType").hide();
        $("#scope").hide();
        $("#scopeTable").hide();
        $.ajax({
            type : "post",
            url : "${basePath}/manage/fonds/list",
            dataType : "json",
            success : function (data) {
                $.each(data,function(i,items){
                    fonds.append("<option value='"+items.fondsId+"'>"+items.fondsNum+"&nbsp;&nbsp;"+items.fondsName+"</option>");
                })
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $.confirm({
                    theme: 'dark',
                    animation: 'rotateX',
                    closeAnimation: 'rotateX',
                    title: false,
                    content: "请求错误",
                    buttons: {
                        confirm: {
                            text: '确认',
                            btnClass: 'waves-effect waves-button waves-light'
                        }
                    }
                });
            }
        },JSON);
    });
//下拉框宽度自适应

    window.onresize = function () {
        var fondsWidth = $("#main").width();
        $(".select2").css("width",fondsWidth+"px");

    }
    var sortNum1 = $("#type1");
    var sortNum2 = $("#type2");
    var sortName = $("#type3");
    //选择某个全宗，遍历其对应实体分类
    function changFonds() {
        var fonds = $("#fonds");
        $("#saveType").hide();
        $("#updateType").hide();
        $.fn.zTree.destroy();
        if(fonds.val() != ''){
        $.ajax({
            type : "post",
            url : "${basePath}/manage/archivestype/list/"+fonds.val(),
            dataType : "json",
            success : function (data) {
                selectType(data);
                sortNum1.val('');
                sortNum1.css("background-color","#F0F0F0");
                sortNum2.val('');
                sortNum2.css("background-color","#F0F0F0");
                sortName.val('');
                sortName.css("background-color","#F0F0F0");

            }
        });
        }
    }
    var zNodes = [];
    function selectType(data) {
        var setting = {
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pid",
                    rootPId: 0
                }
            },
            async: {
                enable: true
            },
            // 回调函数
            callback : {
                onClick: clickZtree

            }
        };
        var zNodes = data;
        $.fn.zTree.init($("#tree"), setting,zNodes);
    }
    //zTree点击事件
    function clickZtree(event, treeId, treeNode, clickFlag){
        //查看前先清空table
        $("#scopeTable tr:not(:first)").empty("");
        if (treeNode.id != '0'){
            $("#saveType").hide();
            $("#updateType").show();
            // 判断是否父节点
            if(!treeNode.isParent){
                //加载归档范围
                //selectScope();
                var pNode = treeNode.getParentNode();
                //$("#scope").show();
                if(pNode.id ==0){
                    numName =  treeNode.name.split("-");
                    sortName.removeAttr("readonly");
                    sortName.css("background-color","#FFFFFF");
                    sortNum1.val('');
                    sortNum2.val(numName[0]);
                    sortName.val(numName[1]);
                }else{
                    pNumName = pNode.name.split("-");
                    numName =  treeNode.name.split("-");
                    sortName.removeAttr("readonly");
                    sortName.css("background-color","#FFFFFF");
                    sortNum1.val(pNumName[0]);
                    sortNum2.val(numName[0]) ;
                    sortName.val(numName[1]) ;
                }

            }else{
                //$("#scope").hide();
                numName =  treeNode.name.split("-");
                /*sortNum2.removeAttr("readonly");
                sortNum2.css("background-color","#FFFFFF");*/
                sortName.removeAttr("readonly");
                sortName.css("background-color","#FFFFFF");
                sortNum1.val('');
                sortNum2.val(numName[0]);
                sortName.val(numName[1]);
            }
        }else{
            $("#saveType").hide();
            $("#updateType").hide();
            //$("#scope").hide();
            sortName.removeAttr("readonly");
            sortName.css("background-color","#FFFFFF");
            sortNum1.val('');
            sortNum2.val('');
            sortName.val('');
        }
    }

    //添加分类
    function insertType(){
        var fondsId = $("#fonds").val();
        if (fondsId != ""){
            //获取tree
            var treeObj = $.fn.zTree.getZTreeObj("tree");
            //获取选中的节点
            var nodes = treeObj.getSelectedNodes();
            if (nodes !=''&& nodes.length>0){
                $("#saveType").show();
                $("#updateType").hide();
                if (nodes[0].id == 0){
                    if (nodes[0].isParent){
                       var sortAndName = nodes[0].children[nodes[0].children.length-1].name.split("-");
                        var nums = eval(sortAndName[0]+"+"+'1');
                        if(nums<10){
                            sortNum2.val('0'+nums);
                        }else{
                            sortNum2.val(nums);
                        }
                    }else{
                        sortNum2.val("01");
                    }

                }else {
                    var sortAndName = nodes[0].name.split("-");
                    sortNum1.val(sortAndName[0]);
                    if (!nodes[0].isParent){
                        sortNum1.val(sortAndName[0]);
                        sortNum2.val(sortAndName[0]+"01");
                        sortName.val('');
                    }else{
                        var sortAndName2 = nodes[0].children[nodes[0].children.length-1].name.split("-");
                        var nums = eval(sortAndName2[0].substring(1)+"+"+'1');
                        sortNum1.val(sortAndName[0]);
                        sortNum2.val(sortAndName[0]+"0"+nums);
                        sortName.val('');
                    }
                }

            }else{
                $.confirm({
                    title: false,
                    content: '请选择目录！',
                    autoClose: 'cancel|3000',
                    backgroundDismiss: true,
                    buttons: {
                        cancel: {
                            text: '取消',
                            btnClass: 'waves-effect waves-button'
                        }
                    }
                });
            }
        }else{
            $.confirm({
                title: false,
                content: '请选择全宗！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '确定',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        }
    }
    //保存分类
    function saveType() {
        //当前全宗号，当前点击节点id，上级序号，当前序号，当前名称
        var fondsId = $("#fonds").val();
        //获取tree
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        //获取选中的节点
        var nodes = treeObj.getSelectedNodes();
        //当前节点id
        var id = nodes[0].id;
        //上级序号
        var num1 = sortNum1.val();
        //当前序号
        var num2 = sortNum2.val();
        if(num2 == ""){
            reConfirm("当前编号不能为空");
            return;
        }
        //当前名称
        var name =sortName.val();


                if(name !=null && name.length >0){
                    $.ajax({
                        type : "post",
                        url : "${basePath}/manage/archivestype/create?fondsId="+fondsId+"&sortNum="+num2+"&sortName="+name+"&sortNum1="+sortNum1.val()+"&id="+id,
                        dataType : "json",
                        contentType: "application/x-www-form-urlencoded; charset=utf-8",
                        success : function (result) {
                            if (result.code != 1) {
                                if (result.data instanceof Array) {
                                    $.each(result.data, function(index, value) {
                                        $.confirm({
                                            theme: 'dark',
                                            animation: 'rotateX',
                                            closeAnimation: 'rotateX',
                                            title: false,
                                            content: value.errorMsg,
                                            buttons: {
                                                confirm: {
                                                    text: '确认',
                                                    btnClass: 'waves-effect waves-button waves-light'
                                                }
                                            }
                                        });
                                    });
                                } else {
                                    $.confirm({
                                        theme: 'dark',
                                        animation: 'rotateX',
                                        closeAnimation: 'rotateX',
                                        title: false,
                                        content: result.data.errorMsg,
                                        buttons: {
                                            confirm: {
                                                text: '确认',
                                                btnClass: 'waves-effect waves-button waves-light'
                                            }
                                        }
                                    });
                                }
                            } else {
                                $.confirm({
                                    theme: 'dark',
                                    animation: 'rotateX',
                                    closeAnimation: 'rotateX',
                                    title: false,
                                    content: "添加成功",
                                    buttons: {
                                        confirm: {
                                            text: '确认',
                                            btnClass: 'waves-effect waves-button waves-light'
                                        }
                                    }
                                });
                                changFonds();
                            }
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            $.confirm({
                                theme: 'dark',
                                animation: 'rotateX',
                                closeAnimation: 'rotateX',
                                title: false,
                                content: "请求错误",
                                buttons: {
                                    confirm: {
                                        text: '确认',
                                        btnClass: 'waves-effect waves-button waves-light'
                                    }
                                }
                            });
                        }
                    });
                }else{
                    $.confirm({
                        title: false,
                        content: '类目名称不能为空！',
                        autoClose: 'cancel|3000',
                        backgroundDismiss: true,
                        buttons: {
                            cancel: {
                                text: '确定',
                                btnClass: 'waves-effect waves-button'
                            }
                        }
                    });
                }


    }
    //修改分类
    function updateType(){
        var num1 = sortNum1.val();
        var num2 = sortNum2.val();
        var name = sortName.val();
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        //获取选中的节点
        var nodes = treeObj.getSelectedNodes();
        //当前节点id
        var id = nodes[0].id;
            if (num2 ==""){
                reConfirm("当前编号不能为空");
            }
            if (name!=null && name.length>0){
                $.ajax({
                    type : "post",
                    url : "${basePath}/manage/archivestype/update?archivesId="+nodes[0].id+"&sortNum="+num2+"&sortName="+name,
                    dataType : "json",
                    success : function(result){
                        if (result>0){
                            $.confirm({
                                title: false,
                                content: '修改成功！',
                                autoClose: 'cancel|3000',
                                backgroundDismiss: true,
                                buttons: {
                                    cancel: {
                                        text: '确定',
                                        btnClass: 'waves-effect waves-button'
                                    }
                                }
                            });
                           var cc = nodes[0].name.split("-");
                            cc[0]=num2;
                            cc[1]=name;
                            var node = cc[0]+"-"+cc[1];
                            nodes[0].name = node;
                            treeObj.updateNode(nodes[0]);
                        }
                    },
                    error :function () {
                        $.confirm({
                            title: false,
                            content: '修改失败！',
                            autoClose: 'cancel|3000',
                            backgroundDismiss: true,
                            buttons: {
                                cancel: {
                                    text: '确定',
                                    btnClass: 'waves-effect waves-button'
                                }
                            }
                        });
                    }
                });
            }else{
                $.confirm({
                    title: false,
                    content: '该目录下类目序号格式不正确！',
                    autoClose: 'cancel|3000',
                    backgroundDismiss: true,
                    buttons: {
                        cancel: {
                            text: '确定',
                            btnClass: 'waves-effect waves-button'
                        }
                    }
                });
            }

    }
    //删除分类
    function delType(){
        var fondsId = $("#fonds").val();
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        if (treeObj ==null || treeObj.length<=0){
            return;
        }
        //获取选中的节点
        var nodes = treeObj.getSelectedNodes();
        if (nodes !=''&& nodes.length>0){
            $.confirm({
                title: false,
                content: '确定删除此目录？',
                //autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    ok: {
                        text: '确定',
                        btnClass: 'waves-effect waves-button',
                        action: function (){
                            if (nodes[0].isParent){
                                $.confirm({
                                    title: false,
                                    content: '当前目录下有其他目录，是否删除？',
                                    backgroundDismiss: true,
                                    buttons: {
                                        ok: {
                                            text: '确定',
                                            btnClass: 'waves-effect waves-button',
                                            action: function (){
                                                $.ajax({
                                                    method:"post",
                                                    url:"${basePath}/manage/archivestype/delTypes",
                                                    data:{
                                                        archivesId: nodes[0].id,
                                                        fondsId:fondsId
                                                    },
                                                    success:function (result) {
                                                        if (result == 1){
                                                            changFonds();
                                                            $.confirm({
                                                                title: false,
                                                                content: '删除成功！',
                                                                autoClose: 'cancel|3000',
                                                                backgroundDismiss: true,
                                                                buttons: {
                                                                    cancel: {
                                                                        text: '确定',
                                                                        btnClass: 'waves-effect waves-button'
                                                                    }
                                                                }
                                                            });
                                                        }else if (result ==0){
                                                            $.confirm({
                                                                title: false,
                                                                content: '该分类已被引用，无法删除！',
                                                                backgroundDismiss: true,
                                                                buttons: {
                                                                    cancel: {
                                                                        text: '确定',
                                                                        btnClass: 'waves-effect waves-button'
                                                                    }
                                                                }
                                                            });
                                                        }else if (result == 2){
                                                            $.confirm({
                                                                title: false,
                                                                content: '无法删除，请重试！',
                                                                backgroundDismiss: true,
                                                                buttons: {
                                                                    cancel: {
                                                                        text: '确定',
                                                                        btnClass: 'waves-effect waves-button'
                                                                    }
                                                                }
                                                            });
                                                        }
                                                    }
                                                });
                                            }
                                        },
                                        cancel: {
                                            text: '取消',
                                            btnClass: 'waves-effect waves-button'
                                        }
                                    }
                                });
                            }else{
                                if (nodes[0].id == 0){
                                    $.confirm({
                                        title: false,
                                        content: '此分类不可删除！',
                                        autoClose: 'cancel|3000',
                                        backgroundDismiss: true,
                                        buttons: {
                                            cancel: {
                                                text: '确定',
                                                btnClass: 'waves-effect waves-button'
                                            }
                                        }
                                    });
                                }else{
                                    $.post("${basePath}/manage/archivestype/delTypes", {archivesId: nodes[0].id,fondsId:fondsId}, function(data) {
                                        if (data == 1){
                                            changFonds();
                                            $.confirm({
                                                title: false,
                                                content: '删除成功！',
                                                autoClose: 'cancel|3000',
                                                backgroundDismiss: true,
                                                buttons: {
                                                    cancel: {
                                                        text: '确定',
                                                        btnClass: 'waves-effect waves-button'
                                                    }
                                                }
                                            });
                                            //$("#scopeTable tr:not(:first)").empty("");
                                            //$("#scope").hide();
                                        }else if (data ==0){
                                            $.confirm({
                                                title: false,
                                                content: '该分类已被引用，无法删除！',
                                                backgroundDismiss: true,
                                                buttons: {
                                                    cancel: {
                                                        text: '确定',
                                                        btnClass: 'waves-effect waves-button'
                                                    }
                                                }
                                            });
                                        }else if (data == 2){
                                            $.confirm({
                                                title: false,
                                                content: '无法删除，请重试！',
                                                backgroundDismiss: true,
                                                buttons: {
                                                    cancel: {
                                                        text: '确定',
                                                        btnClass: 'waves-effect waves-button'
                                                    }
                                                }
                                            });
                                        }
                                    });
                                }

                            }
                        }
                    },
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
            //

        }else{
            $.confirm({
                title: false,
                content: '请选择删除的目录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '确定',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        }
    }
function reConfirm(msg){
    $.confirm({
        theme: 'dark',
        animation: 'rotateX',
        closeAnimation: 'rotateX',
        title: '提示',
        content: msg,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'waves-effect waves-button waves-light'
            }
        }
    });
}
</script>
</body>
</html>